﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.UI;
using System.Web.Mvc;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarts.Modules.GoogleMap
{
    public class GoogleMapModule : Module
    {
        private string api_Key = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string API_Key
        {
            get { return api_Key; }
            set { api_Key = value; }
        }

        private System.Web.UI.WebControls.Unit width = 400;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public System.Web.UI.WebControls.Unit Width
        {
            get { return width; }
            set { width = value; }
        }

        private System.Web.UI.WebControls.Unit height = 400;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public System.Web.UI.WebControls.Unit Height
        {
            get { return height; }
            set { height = value; }
        }

        private double center_X = 53.908068;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public double Center_X
        {
            get { return center_X; }
            set { center_X = value; }
        }

        private double center_Y = -1.506606;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public double Center_Y
        {
            get { return center_Y; }
            set { center_Y = value; }
        }

        private int zoom_Level = 9;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public int Zoom_Level
        {
            get { return zoom_Level; }
            set { zoom_Level = value; }
        }


        public override void Initialise()
        {
            this.ModuleName = "Google Map Module";
            this.ModuleDescription = "Places a Google Map on the page.";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string sKeyScript = string.Empty;
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(string.Format("http://maps.google.com/maps?file=api&v=1&key={0}", this.api_Key));
            HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
            System.IO.StreamReader reader = new System.IO.StreamReader(myRes.GetResponseStream());
            string script = reader.ReadToEnd();

            JavaScript.RegisterScript("GMapKey", script, JavaScriptType.PageScript);
            JavaScript.RegisterScript(string.Format("googleMap_{0}", this.ModuleId), InitializeMap(), JavaScriptType.StartupScript);
            
            LiteralControl container = new LiteralControl(string.Format("<div id=\"googleMap_{0}\" style=\"width: {1}; height: {2};\"></div>", this.ModuleId, this.width.ToString(), this.height.ToString()));

            this.Controls.Add(container);

            if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(Permissions.CMSPage.Edit, CMSPage.Current.PageId.ToString()))
            {
                string editBar = "<div>Double click here edit Google Map</div>";
                this.Controls.Add(new LiteralControl(editBar));
            }
        
        }

        private string InitializeMap()
        {
            //StringBuilder script = new StringBuilder();
            string script;

            script= @"if (GBrowserIsCompatible()) {
                        map = new GMap2(document.getElementById('googleMap_" + this.ModuleId + @"'));
                        geocoder = new GClientGeocoder();
                        map.addControl(new GSmallMapControl());
                        map.addControl(new GMapTypeControl());
                        map.setCenter(new GLatLng(" + this.center_X + @", " + this.center_Y + @"), " + this.zoom_Level + @");
                    }";

            return script.ToString();
        }

    }
}
